package com.china08.yxyapi.api.manage.sch.platform.website.article;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.DateToolUtils;

@CrossOrigin
@RestController
public class ArticleListController extends YxyApiBaseController {
    Logger Log = Logger.getLogger(ArticleListController.class);

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/article/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        String section1Id = reqModel.section1Id;
        String section2Id = reqModel.section2Id;
        Integer auditStatus = reqModel.auditStatus;
        String keyWord = reqModel.keyWord;

        // 二级点击的是全部
        if ("all".equals(section2Id)) {
            section2Id = "";
        }
        // 审核点击的是全部
        if (auditStatus != null && auditStatus == -11) {
            auditStatus = null;
        }

        String schoolId = getProfile(request, response).getSchoolId();

        Page<SwsArticle> page = new PageImpl<>(new ArrayList<SwsArticle>());
        Sort sort1 = new Sort(Direction.DESC, "isSticky").and(new Sort(Direction.DESC, "recommend")).and(new Sort(Direction.DESC, "publishDate"));
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size, sort1);

        // 所有
        if (StringUtils.isBlank(section1Id) && StringUtils.isBlank(section2Id) && null == auditStatus && StringUtils.isBlank(keyWord)) {
            Log.info("查询数据：[所有]");
            page = swsArticleRepository.findByOwnerSchoolId(schoolId, pageable);
        }

        // 一级栏目
        else if (StringUtils.isNotBlank(section1Id) && StringUtils.isBlank(section2Id) && null == auditStatus && StringUtils.isBlank(keyWord)) {
            Log.info("查询数据：[一级栏目]");
            // 无一级栏目数据
            if ("noSection1Id".equals(section1Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1Id(schoolId, "", pageable);
            } else {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1Id(schoolId, section1Id, pageable);
            }
        }

        // 一级栏目 + 二级栏目
        else if (StringUtils.isNotBlank(section1Id) && StringUtils.isNotBlank(section2Id) && null == auditStatus && StringUtils.isBlank(keyWord)) {
            Log.info("查询数据：[二级栏目]");
            // 无一级栏目 + 无二级栏目
            if ("noSection1Id".equals(section1Id) && "noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2Id(schoolId, "", "", pageable);
            }
            // 有一级栏目 + 有二级栏目
            else {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2Id(schoolId, section1Id, section2Id, pageable);
            }
        }

        // 一级栏目+ 二级栏目+ 搜索
        else if (StringUtils.isNotBlank(section1Id) && StringUtils.isNotBlank(section2Id) && null == auditStatus && StringUtils.isNotBlank(keyWord)) {
            Log.info("查询数据：[一级栏目+ 二级栏目+ 搜索]");

            // 无一级栏目 + 无二级栏目 + 搜索
            if ("noSection1Id".equals(section1Id) && "noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndTitleContaining(schoolId, "", "", keyWord, pageable);
            }
            // 无一级栏目 + 有二级栏目 + 搜索
            else if ("noSection1Id".equals(section1Id) && !"noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndTitleContaining(schoolId, "", section2Id, keyWord, pageable);
            }
            // 有一级栏目 + 无二级栏目 + 搜索
            else if (!"noSection1Id".equals(section1Id) && "noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndTitleContaining(schoolId, section1Id, "", keyWord, pageable);
            }
            // 有一级栏目 + 有二级栏目 + 搜索
            else if (!"noSection1Id".equals(section1Id) && "!noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndTitleContaining(schoolId, section1Id, section2Id, keyWord, pageable);
            }
        }

        // 一级栏目 + 审核状态
        else if (StringUtils.isNotBlank(section1Id) && StringUtils.isBlank(section2Id) && null != auditStatus && StringUtils.isBlank(keyWord)) {
            // 无一级栏目 + 审核状态
            if ("noSection1Id".equals(section1Id) && null != auditStatus) {
                Log.info("查询数据：[一级栏目 + 审核状态]");
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndAuditStatus(schoolId, "", auditStatus, pageable);
            }
            // 有一级栏目 + 审核状态
            else {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndAuditStatus(schoolId, section1Id, auditStatus, pageable);
            }
        }

        // 一级栏目 + 搜索
        else if (StringUtils.isNotBlank(section1Id) && StringUtils.isBlank(section2Id) && null == auditStatus && StringUtils.isNotBlank(keyWord)) {
            Log.info("查询数据：[一级栏目 + 搜索]");
            // 无一级栏目 + 搜索
            if ("noSection1Id".equals(section1Id) && StringUtils.isNotBlank(keyWord)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndTitleContaining(schoolId, "", keyWord, pageable);
            }
            // 有一级栏目 + 搜索
            else {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndTitleContaining(schoolId, section1Id, keyWord, pageable);
            }
        }
        // 一级栏目 + 审核状态 + 搜索
        else if (StringUtils.isNotBlank(section1Id) && StringUtils.isBlank(section2Id) && null != auditStatus && StringUtils.isNotBlank(keyWord)) {
            Log.info("查询数据：[一级栏目 + 审核状态 + 搜索]");

            // 无一级栏目 + 审核状态 + 搜索
            if ("noSection1Id".equals(section1Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndAuditStatusAndTitleContaining(schoolId, "", auditStatus, keyWord, pageable);
            }
            // 有一级栏目 + 审核状态 + 搜索
            else {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndAuditStatusAndTitleContaining(schoolId, section1Id, auditStatus, keyWord, pageable);
            }
        }
        // 一级栏目 + 二级栏目 + 审核状态
        else if (StringUtils.isNotBlank(section1Id) && StringUtils.isNotBlank(section2Id) && null != auditStatus && StringUtils.isBlank(keyWord)) {
            Log.info("查询数据：[一级栏目 + 二级栏目 + 审核状态]");
            // 无一级栏 + 无二级栏目 + 审核状态
            if ("noSection1Id".equals(section1Id) && "noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndAuditStatus(schoolId, "", "", auditStatus, pageable);
            }
            // 无一级栏目 + 有二级栏目+审核状态
            else if ("noSection1Id".equals(section1Id) && !"noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndAuditStatus(schoolId, "", section2Id, auditStatus, pageable);
            }
            // 有一级栏目 ＋无二级栏目+审核状态
            else if (!"noSection1Id".equals(section1Id) && "noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndAuditStatus(schoolId, section1Id, "", auditStatus, pageable);
            }
            // 有一级栏目 ＋有二级栏目+审核状态
            else if (!"noSection1Id".equals(section1Id) && !"noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndAuditStatus(schoolId, section1Id, section2Id, auditStatus, pageable);
            }
        }

        // 一级栏目 +二级栏目 + 审核状态 + 搜索
        else if (StringUtils.isNotBlank(section1Id) && StringUtils.isNotBlank(section2Id) && null != auditStatus && StringUtils.isNotBlank(keyWord)) {
            Log.info("查询数据：[一级栏目 + 二级栏目 + 审核状态 + 搜索]");
            // 无一级栏目+无二级栏目+审核状态+ 搜索
            if ("noSection1Id".equals(section1Id) && "noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndAuditStatusAndTitleContaining(schoolId, "", "", auditStatus, keyWord, pageable);
            }
            // 有一级栏目+有二级栏目+审核状态+ 搜索
            else if (!"noSection1Id".equals(section1Id) && !"noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndAuditStatusAndTitleContaining(schoolId, section1Id, section2Id, auditStatus, keyWord, pageable);
            }
            // 有一级栏目+无二级栏目+审核状态+ 搜索
            else if (!"noSection1Id".equals(section1Id) && "noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndAuditStatusAndTitleContaining(schoolId, section1Id, "", auditStatus, keyWord, pageable);
            }
            // 无一级栏目+有二级栏目+审核状态+ 搜索
            else if ("noSection1Id".equals(section1Id) && !"noSection2Id".equals(section2Id)) {
                page = swsArticleRepository.findByOwnerSchoolIdAndSection1IdAndSection2IdAndAuditStatusAndTitleContaining(schoolId, "", section2Id, auditStatus, keyWord, pageable);

            }
        }

        // 审核状态
        else if (StringUtils.isBlank(section1Id) && StringUtils.isBlank(section2Id) && null != auditStatus && StringUtils.isBlank(keyWord)) {
            Log.info("查询数据：[审核状态]");
            page = swsArticleRepository.findByOwnerSchoolIdAndAuditStatus(schoolId, auditStatus, pageable);
        }

        // 审核状态 + 搜索
        else if (StringUtils.isBlank(section1Id) && StringUtils.isBlank(section2Id) && null != auditStatus && StringUtils.isNotBlank(keyWord)) {
            Log.info("查询数据：[审核状态 + 搜索]");
            page = swsArticleRepository.findByOwnerSchoolIdAndAuditStatusAndTitleContaining(schoolId, auditStatus, keyWord, pageable);
        }

        // 搜索
        else if (StringUtils.isBlank(section1Id) && StringUtils.isBlank(section2Id) && null == auditStatus && StringUtils.isNotBlank(keyWord)) {
            Log.info("查询数据：[搜索]");
            page = swsArticleRepository.findByOwnerSchoolIdAndTitleContaining(schoolId, keyWord, pageable);
        } else {
            Log.info("无查询");
        }

        List<SwsArticle> swsArticleList = page.getContent();

        List<RespModel> list = new ArrayList<>();
        for (SwsArticle swsArticle : swsArticleList) {
            RespModel respModel = new RespModel();

            String id = swsArticle.getId();
            String publishDate = DateToolUtils.format(swsArticle.getPublishDate(), "yyyy-MM-dd");
            String title = swsArticle.getTitle();
            String sectionId1 = swsArticle.getSection1Id();
            String sectionId2 = swsArticle.getSection2Id();
            String senderName = swsArticle.getSenderName();
            String showDate = DateToolUtils.format(swsArticle.getShowDate(), "yyyy-MM-dd");
            boolean isSticky = swsArticle.getIsSticky();
            boolean recommend = swsArticle.getRecommend();
            Integer audit = swsArticle.getAuditStatus();
            String content = swsArticle.getContent();

            // 获取详情中的所有图片
            Document document = Jsoup.parse(content);
            Elements elements = document.select("img");
            List<String> imgList = new ArrayList<>();
            for (Element element : elements) {
                String src = element.attr("src");
                imgList.add(src);
            }
            respModel.id = id;
            respModel.publishDate = publishDate;
            respModel.title = title;
            respModel.section1Id = sectionId1;
            respModel.section2Id = sectionId2;
            respModel.senderName = senderName;
            respModel.showDate = showDate;
            respModel.isSticky = isSticky;
            respModel.recommend = recommend;
            respModel.auditStatus = audit;
            if (StringUtils.isNotBlank(sectionId2)) {
                // 一级栏目名称
                SwsSection swsSection = swsSectionRepository.findById(sectionId1);
                if (null != swsSection) {
                    // 一级栏目名称
                    respModel.section1Name = swsSection.getSectionName();
                    // 一级栏目是否需要审核
                    respModel.isAudit = swsSection.getIsAudit();
                }

                // 二级栏目名称
                swsSection = swsSectionRepository.findById(sectionId2);
                if (null != swsSection) {
                    respModel.section2Name = swsSection.getSectionName();
                }
            } else {
                // 一级栏目名称
                SwsSection swsSection = swsSectionRepository.findById(sectionId1);
                if (null != swsSection) {
                    respModel.section1Name = swsSection.getSectionName();
                }
            }
            respModel.imgList = imgList;
            list.add(respModel);
        }
        return new PageImpl<RespModel>(list, pageable, page.getTotalElements());
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String section1Id;
        public String section2Id;
        public Integer auditStatus;
        public String keyWord;
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String publishDate;
        public String title;
        public String section1Id;
        public String section2Id;
        public String section1Name;
        public String section2Name;
        public String senderName;
        public String showDate;
        public boolean isSticky;
        public boolean recommend;
        public Integer auditStatus;
        // 当前文章所属栏目是否需要审核
        public boolean isAudit;
        // 详情中的所有图片
        public List<String> imgList;
        public boolean checked = false;
    }
}
